package main

import (
	"fmt"
	"strconv"
)

func main() {
	ch := make(chan int, 10)
	for i := 0; i < 10; i++ {
		ch <- i
	}
	ch2 := make(chan string, 5)
	for i := 0; i < 5; i++ {
		ch2 <- "str" + strconv.Itoa(i)
	}
	// 之前使用 for 遍历时，如果不关闭 channel，会导致阻塞和出现 deadlock
	//可以使用 select 解决
	for {
		over := false
		select {
		case v := <-ch:
			fmt.Println("read from ch:", v)
		case v := <-ch2:
			fmt.Println("read from ch2:", v)
		default:
			fmt.Println("no data")
			over = true
		}
		if over {
			break
		}
	}
}
